﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm14 : System.Web.UI.Page
    {
        subject sub = new subject();
        teacher teach = new teacher(); Classes cl = new Classes();
        protected void Page_Load(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            if (!IsPostBack)
            {
                DataTable dt1 = teach.GetRecords(-99, "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
                ddteacher.DataSource = dt1;
                ddteacher.DataTextField = "teacher_name";
                ddteacher.DataValueField = "teacher_id";
                ddteacher.DataBind();

                DataTable dt2 = cl.GetRecords(-99, "", -99, false, " and ");
                ddclass.DataSource = dt2;
                ddclass.DataTextField = "class_name";
                ddclass.DataValueField = "class_id";
                ddclass.DataBind();

                DataTable dt = sub.GetRecords(-99, "", -99, -99, false, " adn ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            DataTable dt = sub.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, false, " and ");
            tbsubid.Text = dt.Rows[0]["subject_id"].ToString();
            tbsubject.Text = dt.Rows[0]["subject_name"].ToString();
            ddteacher.SelectedValue = dt.Rows[0]["teacher_id"].ToString();
            ddclass.SelectedValue = dt.Rows[0]["class_id"].ToString();
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = sub.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, false, " and ");
                sub.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                sub.subject_name = dt.Rows[0]["subject_name"].ToString();
                sub.teacher_id = Convert.ToInt64(dt.Rows[0]["teacher_id"].ToString());
                sub.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                sub.isdeleted = true;
                sub.SaveRecords();
                Response.Redirect("~/admin/Subject.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            DataTable dt = sub.GetRecords(-99, "", -99, -99, false, " and ");
            if (tbsubject.Text == dt.Rows[0]["subject_name"].ToString() && ddteacher.SelectedValue == dt.Rows[0]["teacher_id"].ToString() && ddclass.SelectedValue == dt.Rows[0]["class_id"].ToString())
            {
                Label1.Text = "Already exist.";
            }
            else
            {
                sub.subject_id = -99;
                sub.subject_name = tbsubject.Text;
                sub.teacher_id = Convert.ToInt64(ddteacher.SelectedValue.ToString());
                sub.class_id = Convert.ToInt64(ddclass.SelectedValue.ToString());
                sub.isdeleted = false;
                sub.SaveRecords();
                Response.Redirect("~/admin/Subject.aspx");
            }

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            sub.subject_id = Convert.ToInt64(tbsubid.Text);
            sub.subject_name = tbsubject.Text;
            sub.teacher_id = Convert.ToInt64(ddteacher.SelectedValue.ToString());
            sub.class_id = Convert.ToInt64(ddclass.SelectedValue.ToString());
            sub.isdeleted = false;
            sub.SaveRecords();
            Response.Redirect("~/admin/Subject.aspx");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void ddteacher_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddclass_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
